﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 监控
{
    public partial class FormUser : Form
    {
        string screenSnap = "screen.jpg";
        DES m_des = new DES();
        public string saveData;

        public bool IsSet{
            get{
                if (string.IsNullOrEmpty(from.Text) ||
                    string.IsNullOrEmpty(passwd.Text) ||
                    string.IsNullOrEmpty(smtpaddr.Text) ||
                    string.IsNullOrEmpty(smtport.Text) ||
                    string.IsNullOrEmpty(to.Text))
                    return false;
                else
                    return true;
            } 
        }
        public FormUser(string config)
        {
            InitializeComponent();
            this.FormClosed += FormUser_FormClosed;
            if (!string.IsNullOrEmpty(config))
            {
                try
                {
                    config = m_des.MD5Decrypt(config);
                    string[] data = config.Split(new char[] { ';' });
                    from.Text = data[0];
                    passwd.Text = data[1];
                    smtpaddr.Text = data[2];
                    smtport.Text = data[3];
                    to.Text = data[4];
                }
                catch { }
            }
        }

        private void FormUser_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                saveData = m_des.MD5Encrypt(from.Text+";"+passwd.Text+";"+
                    smtpaddr.Text+";"+smtport.Text+";"+to.Text);
            }
            catch(Exception ex){
                TopMost = false;
                MessageBox.Show(MessageboxExtension.CloseAfter(3000),ex.Message);
                TopMost = true;
            }
        }
        public void SendEmail(string title, string content)
        {
            try
            {//发送
                MailMessage message = new MailMessage(new MailAddress(from.Text), new MailAddress(to.Text))
                {
                    Subject = "【" + title + "】" + "重启通知",
                    Body = content,
                };
                CopyScreen();
                if (File.Exists(screenSnap))
                {
                    Attachment acc = new Attachment(screenSnap);
                    message.Attachments.Add(acc);
                }
                new SmtpClient(smtpaddr.Text)
                {
                    Port = 25,
                    Credentials = new NetworkCredential()
                    {
                        UserName = from.Text,
                        Password = passwd.Text
                    }
                }.Send(message);
                if (File.Exists(screenSnap))    File.Delete(screenSnap);
            }
            catch (Exception ex)
            {
                TopMost = false;
                MessageBox.Show(MessageboxExtension.CloseAfter(3000),ex.Message);
                TopMost = true;
            }
        }
        public void CopyScreen()
        {
            Bitmap myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            using (Graphics g = Graphics.FromImage(myImage))
            {
                g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
                using (FileStream file = File.OpenWrite(screenSnap))
                    myImage.Save(file, ImageFormat.Jpeg);
            }
        }
    }
}
